import requests, time
import aiohttp, asyncio

# 1.使用requests获取单个网址
r=requests.get(url="https://www.liaoxuefeng.com/discuss/001409195742008d822b26cf3de46aea14f2b7378a1ba91000?page=1")
print(len(r.text))   #结果 40007   用时0.3s

# 使用 aiohttp+asyncio 获取单个网址
async def fn():
    async with aiohttp.get(
            url='https://www.liaoxuefeng.com/discuss/001409195742008d822b26cf3de46aea14f2b7378a1ba91000?page=1') as resp:
        text = await resp.text()
        print(len(text))


loop = asyncio.get_event_loop()
loop.run_until_complete(fn())  # 结果 40007   用时0.3s


# 使用requests获取50个网址
a=[len(requests.get(url="https://www.liaoxuefeng.com/discuss/001409195742008d822b26cf3de46aea14f2b7378a1ba91000?page={}".format(i)).text) for i in range(1,51)]
print(a)


# 4.使用aiohttp+asyncio获取50个网址
async def fn(num):
    async with aiohttp.get(
            url='https://www.liaoxuefeng.com/discuss/001409195742008d822b26cf3de46aea14f2b7378a1ba91000?page={}'.format(
                    num)) as resp:
        text = await resp.text()
        result.append(len(text))


result = []
loop = asyncio.get_event_loop()
tasks = [asyncio.ensure_future(fn(i)) for i in range(1, 51)]
loop.run_until_complete(asyncio.wait(tasks))
print(result)


